/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xj.marqueeview.sample.slice;

import com.xj.marqueeview.AnimationUtils;
import com.xj.marqueeview.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.TabList;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.Arrays;
import java.util.List;

/**
 * 动画演示类
 *
 * @author:Hexy
 * @since 2021-03-24
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener,
        Component.BindStateChangedListener {
    /**
     * log日志标签
     */
    public static final HiLogLabel LOGLABEL = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainAbilitySlice");

    /**
     * table0
     */
    public static final int TABLE_ZERO_POS = 0;

    /**
     * table1
     */
    public static final int TABLE_ONE_POS = 1;
    /**
     * table2
     */
    public static final int TABLE_TWO_POS = 2;
    /**
     * table3
     */
    public static final int TABLE_THREE_POS = 3;
    private Text textOne;
    private Text textTwo;
    private Text textThree;
    private Text textFour;

    private Image ivOne;
    private Image ivTwo;
    private Image ivThree;
    private Image ivFour;

    private DirectionalLayout dirUpandDown;
    private DirectionalLayout dirDownandUp;
    private DirectionalLayout dirLeftandRight;
    private DirectionalLayout dirRightandLeft;

    private AnimatorGroup upAnimatorGroup;
    private AnimatorGroup downAnimatorGroup;
    private AnimatorGroup leftAnimatorGroup;
    private AnimatorGroup rightAnimatorGroup;

    private int pos1 = 0;
    private int pos2 = 0;
    private int pos3 = 0;
    private int pos4 = 0;

    /**
     * 当前第几次滚动
     */
    private int pos = 0;

    /**
     * 当前选中哪个选项卡
     */
    private int tabPos = 0;

    private List<String> tips1 = Arrays.asList("下终南山过斛斯山人宿置酒  【作者】李白 【朝代】唐",
            "暮从碧山下，山月随人归。",
            "却顾所来径，苍苍横翠微。",
            "相携及田家，童稚开荆扉。",
            "绿竹入幽径，青萝拂行衣。",
            "欢言得所憩，美酒聊共挥。",
            "长歌吟松风，曲尽河星稀。",
            "我醉君复乐，陶然共忘机。"
    );
    private List<String> tips2 = Arrays.asList("行路难·其一 【作者】李白 【朝代】唐",
            "金樽清酒斗十千，玉盘珍羞直万钱。",
            "停杯投箸不能食，拔剑四顾心茫然。",
            "欲渡黄河冰塞川，将登太行雪满山。",
            "闲来垂钓碧溪上，忽复乘舟梦日边。",
            "行路难，行路难，多歧路，今安在？",
            "长风破浪会有时，直挂云帆济沧海。"
    );
    private List<String> tips3 = Arrays.asList("南陵别儿童入京 【作者】李白 【朝代】唐",
            "白酒新熟山中归，黄鸡啄黍秋正肥。",
            "呼童烹鸡酌白酒，儿女嬉笑牵人衣。",
            "高歌取醉欲自慰，起舞落日争光辉",
            "游说万乘苦不早，著鞭跨马涉远道。",
            "会稽愚妇轻买臣，余亦辞家西入秦。",
            "仰天大笑出门去，我辈岂是蓬蒿人。"
    );
    private List<String> tips4 = Arrays.asList("送友人 【作者】李白 【朝代】唐",
            "青山横北郭，白水绕东城。",
            "此地一为别，孤蓬万里征。",
            "浮云游子意，落日故人情。",
            "挥手自兹去，萧萧班马鸣。"
    );

    /**
     * onStart
     *
     * @param intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initView();
        TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        TabList.Tab tab = tabList.new Tab(getContext());
        tab.setText("SimpleText");
        tabList.addTab(tab);
        tabList.selectTab(tab);

        tab = tabList.new Tab(getContext());
        tab.setText("ImageText");
        tabList.addTab(tab);

        tab = tabList.new Tab(getContext());
        tab.setText("MultiText");
        tabList.addTab(tab);

        tab = tabList.new Tab(getContext());
        tab.setText("AllType");
        tabList.addTab(tab);

        dirUpandDown.setClickedListener(this);
        dirDownandUp.setClickedListener(this);
        dirLeftandRight.setClickedListener(this);
        dirRightandLeft.setClickedListener(this);

        dirUpandDown.setBindStateChangedListener(this);
        dirDownandUp.setBindStateChangedListener(this);
        dirLeftandRight.setBindStateChangedListener(this);
        dirRightandLeft.setBindStateChangedListener(this);

        setData(textOne, tips1.get(pos1));
        setData(textTwo, tips2.get(pos2));
        setData(textThree, tips3.get(pos3));
        setData(textFour, tips4.get(pos4));

        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tabView) {
                // 当某个Tab从未选中状态变为选中状态时的回调
                pos = TABLE_ZERO_POS;
                tabPos = tabView.getPosition();
                setTabData();
                startAllAnimator();
            }

            @Override
            public void onUnselected(TabList.Tab tabView) {
            }

            @Override
            public void onReselected(TabList.Tab tabView) {
            }
        });
    }

    /**
     * 初始化view
     */
    private void initView() {
        textOne = (Text) findComponentById(ResourceTable.Id_text_one);
        textTwo = (Text) findComponentById(ResourceTable.Id_text_two);
        textThree = (Text) findComponentById(ResourceTable.Id_text_three);
        textFour = (Text) findComponentById(ResourceTable.Id_text_four);

        ivOne = (Image) findComponentById(ResourceTable.Id_iv_one);
        ivTwo = (Image) findComponentById(ResourceTable.Id_iv_two);
        ivThree = (Image) findComponentById(ResourceTable.Id_iv_three);
        ivFour = (Image) findComponentById(ResourceTable.Id_iv_four);

        dirUpandDown = (DirectionalLayout) findComponentById(ResourceTable.Id_dir_up_and_down);
        dirDownandUp = (DirectionalLayout) findComponentById(ResourceTable.Id_dir_down_and_up);
        dirLeftandRight = (DirectionalLayout) findComponentById(ResourceTable.Id_dir_left_and_right);
        dirRightandLeft = (DirectionalLayout) findComponentById(ResourceTable.Id_dir_right_and_left);
    }

    /**
     * onClick
     *
     * @param component
     */
    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_dir_up_and_down:
                if (upAnimatorGroup.isRunning()) {
                    upAnimatorGroup.stop();
                } else {
                    startUpAnimator();
                }

                break;
            case ResourceTable.Id_dir_down_and_up:
                if (downAnimatorGroup.isRunning()) {
                    downAnimatorGroup.stop();
                } else {
                    startDownAnimator();
                }
                break;
            case ResourceTable.Id_dir_left_and_right:
                if (leftAnimatorGroup.isRunning()) {
                    leftAnimatorGroup.stop();
                } else {
                    startLeftAnimator();
                }

                break;
            case ResourceTable.Id_dir_right_and_left:
                if (rightAnimatorGroup.isRunning()) {
                    rightAnimatorGroup.stop();
                } else {
                    startRightAnimator();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 从上进入，从下退出
     */
    private void startUpAnimator() {
        if (upAnimatorGroup != null) {
            upAnimatorGroup.stop();
            upAnimatorGroup = null;
        }
        upAnimatorGroup = AnimationUtils.setAnimator(AnimationUtils.getUpInAnimator(dirUpandDown),
                AnimationUtils.getDownOutAnimator(dirUpandDown));
        upAnimatorGroup.setLoopedListener(new Animator.LoopedListener() {
            @Override
            public void onRepeat(Animator animator) {
                pos1++;
                if (pos1 >= tips1.size()) {
                    pos1 = 0;
                }
                setData(textOne, tips1.get(pos1));
            }
        });
        upAnimatorGroup.start();
    }

    /**
     * 从下进入，从上退出
     */
    private void startDownAnimator() {
        if (downAnimatorGroup != null) {
            downAnimatorGroup.stop();
            downAnimatorGroup = null;
        }
        downAnimatorGroup = AnimationUtils.setAnimator(AnimationUtils.getDownInAnimator(dirDownandUp),
                AnimationUtils.getUpOutAnimator(dirDownandUp));
        downAnimatorGroup.setLoopedListener(new Animator.LoopedListener() {
            @Override
            public void onRepeat(Animator animator) {
                pos2++;
                if (pos2 >= tips2.size()) {
                    pos2 = 0;
                }
                setData(textTwo, tips2.get(pos2));
            }
        });
        downAnimatorGroup.start();
    }

    /**
     * 从左进入，从右退出
     */
    private void startLeftAnimator() {
        if (leftAnimatorGroup != null) {
            leftAnimatorGroup.stop();
            leftAnimatorGroup = null;
        }
        leftAnimatorGroup = AnimationUtils.setAnimator(AnimationUtils.getLeftInAnimator(dirLeftandRight),
                AnimationUtils.getRightOutAnimator(dirLeftandRight));
        leftAnimatorGroup.setLoopedListener(new Animator.LoopedListener() {
            @Override
            public void onRepeat(Animator animator) {
                pos3++;
                if (pos3 >= tips3.size()) {
                    pos3 = 0;
                }
                setData(textThree, tips3.get(pos3));
            }
        });
        leftAnimatorGroup.start();
    }

    /**
     * 从右进入，从左退出
     */
    private void startRightAnimator() {
        if (rightAnimatorGroup != null) {
            rightAnimatorGroup.stop();
            rightAnimatorGroup = null;
        }
        rightAnimatorGroup = AnimationUtils.setAnimator(AnimationUtils.getRightInAnimator(dirRightandLeft),
                AnimationUtils.getLeftOutAnimator(dirRightandLeft));
        rightAnimatorGroup.setLoopedListener(new Animator.LoopedListener() {
            @Override
            public void onRepeat(Animator animator) {
                pos4++;
                if (pos4 >= tips4.size()) {
                    pos4 = 0;
                }
                setData(textFour, tips4.get(pos4));
            }
        });
        rightAnimatorGroup.start();
    }

    /**
     * 填充数据并设置跑马灯效果
     *
     * @param text
     * @param tips
     */
    private void setData(Text text, String tips) {
        HiLog.info(LOGLABEL, tabPos + "");
        text.setText(tips);

        // 跑马灯效果
        text.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);

        // 始终处于自动滚动状态
        text.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);

        // 启动跑马灯效果
        text.startAutoScrolling();
        setTabData();
    }

    private void setTabData() {
        switch (tabPos) {
            case TABLE_ZERO_POS:
                setSimpleText();
                break;
            case TABLE_ONE_POS:
                setImageText();
                break;
            case TABLE_TWO_POS:
                setMultiText();
                break;
            case TABLE_THREE_POS:
                setAllType();
                break;
            default:
                break;
        }
    }

    /**
     * 设置SimpleText的表现形式
     */
    private void setSimpleText() {
        ivOne.setVisibility(Component.HIDE);
        ivTwo.setVisibility(Component.HIDE);
        ivThree.setVisibility(Component.HIDE);
        ivFour.setVisibility(Component.HIDE);
    }

    /**
     * 设置ImageText的表现形式
     */
    private void setImageText() {
        ivOne.setVisibility(Component.VISIBLE);
        ivTwo.setVisibility(Component.VISIBLE);
        ivThree.setVisibility(Component.VISIBLE);
        ivFour.setVisibility(Component.VISIBLE);
        switch (pos) {
            case TABLE_ZERO_POS:
                ivOne.setPixelMap(ResourceTable.Media_ic_notification);
                ivTwo.setPixelMap(ResourceTable.Media_notice);
                ivThree.setPixelMap(ResourceTable.Media_doubt);
                ivFour.setPixelMap(ResourceTable.Media_notice);
                pos = TABLE_ONE_POS;
                break;
            case TABLE_ONE_POS:
                ivOne.setPixelMap(ResourceTable.Media_doubt);
                ivTwo.setPixelMap(ResourceTable.Media_notice);
                ivThree.setPixelMap(ResourceTable.Media_ic_notification);
                ivFour.setPixelMap(ResourceTable.Media_doubt);
                pos = TABLE_TWO_POS;
                break;
            case TABLE_TWO_POS:
                ivOne.setPixelMap(ResourceTable.Media_notice);
                ivTwo.setPixelMap(ResourceTable.Media_ic_notification);
                ivThree.setPixelMap(ResourceTable.Media_doubt);
                ivFour.setPixelMap(ResourceTable.Media_ic_notification);
                pos = TABLE_ZERO_POS;
                break;
            default:
                break;
        }
    }

    /**
     * 设置MultiText的表现形式
     */
    private void setMultiText() {
        switch (pos) {
            case TABLE_ZERO_POS:
                ivOne.setVisibility(Component.HIDE);
                ivTwo.setVisibility(Component.HIDE);
                ivThree.setVisibility(Component.HIDE);
                ivFour.setVisibility(Component.HIDE);
                pos = TABLE_ONE_POS;
                break;
            case TABLE_ONE_POS:
                ivOne.setVisibility(Component.VISIBLE);
                ivTwo.setVisibility(Component.VISIBLE);
                ivThree.setVisibility(Component.VISIBLE);
                ivFour.setVisibility(Component.VISIBLE);
                ivOne.setPixelMap(ResourceTable.Media_doubt);
                ivTwo.setPixelMap(ResourceTable.Media_doubt);
                ivThree.setPixelMap(ResourceTable.Media_doubt);
                ivFour.setPixelMap(ResourceTable.Media_doubt);
                pos = TABLE_TWO_POS;
                break;
            case TABLE_TWO_POS:
                ivOne.setVisibility(Component.VISIBLE);
                ivTwo.setVisibility(Component.VISIBLE);
                ivThree.setVisibility(Component.VISIBLE);
                ivFour.setVisibility(Component.VISIBLE);
                ivOne.setPixelMap(ResourceTable.Media_notice);
                ivTwo.setPixelMap(ResourceTable.Media_notice);
                ivThree.setPixelMap(ResourceTable.Media_notice);
                ivFour.setPixelMap(ResourceTable.Media_notice);
                pos = TABLE_ZERO_POS;
                break;
            default:
                break;
        }
    }

    /**
     * 设置AllType的表现形式
     */
    private void setAllType() {
        ivTwo.setVisibility(Component.HIDE);
        switch (pos) {
            case TABLE_ZERO_POS:
                ivOne.setVisibility(Component.HIDE);
                ivThree.setVisibility(Component.VISIBLE);
                ivFour.setVisibility(Component.VISIBLE);
                ivThree.setPixelMap(ResourceTable.Media_doubt);
                ivFour.setPixelMap(ResourceTable.Media_notice);
                pos = TABLE_ONE_POS;
                break;
            case TABLE_ONE_POS:
                ivOne.setVisibility(Component.VISIBLE);
                ivThree.setVisibility(Component.HIDE);
                ivFour.setVisibility(Component.VISIBLE);
                ivOne.setPixelMap(ResourceTable.Media_doubt);
                ivThree.setPixelMap(ResourceTable.Media_notice);
                ivFour.setPixelMap(ResourceTable.Media_notice);
                pos = TABLE_TWO_POS;
                break;
            case TABLE_TWO_POS:
                ivOne.setVisibility(Component.VISIBLE);
                ivThree.setVisibility(Component.VISIBLE);
                ivFour.setVisibility(Component.VISIBLE);
                ivOne.setPixelMap(ResourceTable.Media_notice);
                ivThree.setPixelMap(ResourceTable.Media_notice);
                ivFour.setPixelMap(ResourceTable.Media_ic_notification);

                pos = TABLE_ZERO_POS;
                break;
            default:
                break;
        }
    }

    /**
     * onComponentBoundToWindow
     *
     * @param component
     */
    @Override
    public void onComponentBoundToWindow(Component component) {
        startAllAnimator();
    }

    /**
     * 启动所有动画
     */
    private void startAllAnimator() {
        startUpAnimator();
        startDownAnimator();
        startLeftAnimator();
        startRightAnimator();
    }

    /**
     * onComponentUnboundFromWindow
     *
     * @param component
     */
    @Override
    public void onComponentUnboundFromWindow(Component component) {
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     *
     * @param intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * onStop
     */
    @Override
    protected void onStop() {
        super.onStop();
    }
}
